<html>
<head>
<title>Promotion</title>
</head>

<body>

<center>
<h1>POI VII Stage 3 Problem 6</h1>
<h1>Promotion</h1>
</center>

<p>
Great Bytelandish net of supermarkets asked you for writing a program
simulating costs of the promotion being prepared.
</p>

<p>
The promotion has to obey the following rules: 
</p>
<ul>
<li>A customer, who wants to participate in the promotion, writes on
 the bill, paid by himself, his personal details and throws it to a
 special ballot box.
<li>At the end of every day of the promotion, two bills are taken out 
 from the ballot box:
  <ul>
    <li> the first bill amounting to the greatest sum is chosen,
    <li> then the bill amounting to the least sum is chosen;
  </ul>
The customer, who has paid the greatest bill, gets a money prize
equal to the difference between the sum on his bill and the sum on
the bill amounting to the least sum.
<li>
To avoid multiple prizes for one purchase, both bills selected
accordingly to the above rules, do not return to the ballot
box, but all remaining bills still participate in promotion. 
</ul>

<p>
Turnovers of the supermarket are very big, thus an assumption can be
made, that at the end of every day, before taking out bills amounting to
the greatest and the least sum, there are at least 2 bills in the ballot
box.
</p>

<p>
Your task is to compute on the basis of information about
prices on bills thrown to the ballot box on each day of promotion, what will
be the total cost of prizes during the whole promotion. 
</p>

<h2>Task</h2>
<p>
   Write a program, which:
</p>
<ul>
 <li>reads from the text file <tt>PRO.IN</tt> a list of prices on bills
   thrown to the ballot box on each day of the promotion, 
 <li>computes the total cost of prizes paid in consecutive days of 
   promotion, 
 <li>writes the result to the text file <tt>PRO.OUT</tt>. 
</ul>

<h2>Input</h2>
<p>
The first line of the text file <tt>PRO.IN</tt> contains one positive
integer <i>n</i>, where 1 &lt;= <i>n</i> &lt;= 5000, which is the
duration of promotion in days. 
</p>

<p>
Each of the next <i>n</i> lines consists of a sequence of non-negative
integers separated by single spaces. Numbers in the <i>(i+1)</i>-th line
of the file  represent prices on bills thrown to the ballot box on
the <i>i</i>-th day of promotion. The first integer in the line is <i>k</i>, 
0 &lt;= <i>k</i> &lt;= 10^5,
the number of bills from the day, and the next <i>k</i> numbers are
positive integers standing for the prices on bills; none of these
numbers is greater than <i>10^6</i>. 
</p>

<p>
The total number of bills thrown to the ballot box during the whole
promotion does not exceed <i>10^6</i>. 
</p>

<h2>Output</h2>
<p>
The text file <tt>PRO.OUT</tt> should contain exactly one integer,
which is equal to the total cost of prizes paid  during the whole
promotion. 
</p>

<h2>Sample Input</h2>
<pre>
5
3 1 2 3
2 1 1
4 10 5 5 1
0
1 2
</pre>

<h2>Sample Output</h2>
<pre>
19
</pre>

</body>

</html>
